package com.example.enterprisemanager.mvvm.fragment.allocate.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.enterprisemanager.R;
import com.orhanobut.logger.Logger;

import java.util.Objects;

public class MyDialogAllocate extends DialogFragment {

    private String title;
    public OnDialogGetContext onBtnGetEdit;
    private String[] department = {"采购部",  "研发部", "后勤部", "销售部","财务部",};
    private int id = 0;

    public void show(String title, OnDialogGetContext onBtnGetEdit, FragmentManager fragmentManager) {
        this.title = title;
        this.onBtnGetEdit = onBtnGetEdit;
        show(fragmentManager, "ButtonDialogFragment");
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_dialog_allocate, null);
        EditText edtNum = view.findViewById(R.id.edt_num_dialog);

        RadioButton rbtn1 = view.findViewById(R.id.rbtn_caigou);
        RadioGroup group = view.findViewById(R.id.radioGroup);
        rbtn1.setChecked(true);

        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {
                    case R.id.rbtn_caigou:
                        id = 0;
                        break;
                    case R.id.rbtn_yanfa:
                        id = 1;
                        break;
                    case R.id.rbtn_houqin:
                        id = 2;
                        break;
                    case R.id.rbtn_xiaoshou:
                        id = 3;
                        break;
                    case R.id.rbtn_caiwu:
                        id = 4;
                        break;
                    default:
                        break;
                }
                Logger.d("id = " + id);

            }
        });

        view.findViewById(R.id.btn_ok_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBtnGetEdit.getEditContext(edtNum.getText().toString(), department[id]);
                dismiss();
            }
        });

        view.findViewById(R.id.btn_cancel_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        return new AlertDialog.Builder(Objects.requireNonNull(getActivity()))
                .setView(view)
                .setTitle(title)
                .create();
    }
}
